/*
	* --------------------------------------------------------------------------
	*
  * Copyright 2011 Matt West
	*
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
	*
  *   http://www.apache.org/licenses/LICENSE-2.0
	*
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  * --------------------------------------------------------------------------
*/

$(function() {
	// Add event listeners to the new project form and delete buttons
	setUpListeners();
	
	// Add the current projects to the projects list
	populateProjectsList();
});


/*
*
*/
function setUpListeners() {
	// Catch the form submission
	$("#addProject").submit(function(e) {
		e.preventDefault(); // Stop the form sending
		
		// Get the new project title
		var newProject = $("#project").val();
		
		// Add the new project to the projects array
		addProject(newProject);
		
		$("#project").val("");
	});
	
	// Set up listeners for delete buttons
	$(".deleteProject").live('click', function() {
		
		var answer = confirm("Are you sure?");
		
		if (answer) {
			deleteProject(this.id);
		}
		
	});
	
}


/*
*
*/
function addProject(newProject) {
	// TODO: Should check that the project name is valid
	
	var projectsArray = localStorage['projects'];
	var projects = [];
	
	if (projectsArray != undefined && projectsArray.length != 0) {
		projects = JSON.parse(projectsArray);
	}
	
	// Create an array for the new project
	var project = [newProject];
	
	// Add the new aproject to the projects array
	projects.push(project);
	
	// Save the projects array
	localStorage['projects'] = JSON.stringify(projects);
	
	// Refresh the projects list
	// TODO: Just add the new project dont refresh the whole thing
	populateProjectsList();
	
	// Send a message to the background page to get it to refresh the issues.
	chrome.extension.sendRequest({cmd: "refresh"}, function(response) {
		console.log(response.status);
	});
}

/*
*
*/
function deleteProject(rawId) {
	var textId = rawId.replace('project','');
	var id = parseInt(textId);
	
	$('#' + rawId).parent().remove();
	
	var projects = JSON.parse(localStorage['projects']);
	
	projects.splice(id,1);
	
	// Save the projects array
	localStorage['projects'] = JSON.stringify(projects);
	
	// Send a message to the background page to get it to update the badge.
	chrome.extension.sendRequest({cmd: "badge"}, function(response) {
		console.log(response.status);
	});
}


/*
*	
*/
function populateProjectsList() {
	
	var projectsList = $("#projectsList");
	projectsList.html(''); // Clear the list contents
	
	var projectsArray = localStorage['projects'];
	var projects = [];
	
	if (projectsArray != undefined && projectsArray.length != 0) {
		projects = JSON.parse(projectsArray);
	}
	
	for (var i = 0; i < projects.length; i++) {
		var project = projects[i];
		var html = '<li>' + project[0] + ' - <a href="#" id="project' + i + '" class="deleteProject">Delete</a></li>';
		
		projectsList.append(html);
	}
	
}
